//
//  AppDelegate.m
//  gentleman
//
//  Created by 范月成 on 2017/12/5.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "AppDelegate.h"
#import "GMTabBarControllerConfig.h"
#import "VendorHeader.h"
#import "CommonDefines.h"
#import "GMTouchIDViewController.h"
#import "API.h"

@interface AppDelegate ()

@property (nonatomic, strong) GMTabBarControllerConfig *TabBarConfig;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self configKeyWindow];
    
    [self configNetworking];
    
    return YES;
}

#pragma mark - method
- (void)configKeyWindow {
    self.window.rootViewController = self.TabBarConfig.tabBarController;
}

- (void)configNetworking {
    [XMCenter setupConfig:^(XMConfig *config) {
        config.generalServer = kGentlManHostOne;
        config.callbackQueue = dispatch_get_main_queue();
        config.engine = [XMEngine sharedEngine];
#ifdef DEBUG
        config.consoleLog = NO;
#endif
    }];
    
    [XMCenter setRequestProcessBlock:^(XMRequest *request) {
        dispatch_async_on_main_queue(^{
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        });
    }];
 
    [XMCenter setResponseProcessBlock:^(XMRequest *request, id responseObject, NSError *__autoreleasing *error) {
        dispatch_async_on_main_queue(^{
             [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        });
    }];
}

#pragma mark - getter
- (UIWindow *)window {
    if (!_window) {
        _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _window.backgroundColor = [UIColor whiteColor];
        [_window makeKeyAndVisible];
    }
    return _window;
}

- (GMTabBarControllerConfig *)TabBarConfig {
    if (!_TabBarConfig) {
        _TabBarConfig = [[GMTabBarControllerConfig alloc] init];
    }
    return _TabBarConfig;
}


#pragma mark - app delegate
- (void)applicationWillResignActive:(UIApplication *)application {}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[GMProtectTool shareTool] recordEnterBackgroundTime:[[NSProcessInfo processInfo] systemUptime]];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    if ([UserDefaultsForKey(@"GentleMode") boolValue]) {
        if ([[GMProtectTool shareTool] isEnableTouchID]) {
            NSTimeInterval beginTime = [GMProtectTool shareTool].time;
            NSTimeInterval endTime = [[NSProcessInfo processInfo] systemUptime];
            if (endTime - beginTime > 60) {
                UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
                if (![vc isKindOfClass:[GMTouchIDViewController class]]) {
                    GMTouchIDViewController *touchID = [GMTouchIDViewController new];
                    [vc presentViewController:touchID
                                     animated:nil completion:nil];
                }
            }
        }
    }
}

- (void)applicationDidBecomeActive:(UIApplication *)application {}

- (void)applicationWillTerminate:(UIApplication *)application {}

@end
